package com.three.service.impl;

import com.baor.baseUtil.R;
import com.three.dao.ClassesDao;
import com.three.domain.Classes;
import com.three.domain.Students;
import com.three.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClassesServiceImpl implements ClassesService {
    //总端
    @Autowired
    private ClassesDao classesDao;

    // 查询班级列表
    @Override
    public R classesList(String keyword, String start_date, String end_date, Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountClasses = classesDao.PageCountClasses(keyword,start_date,end_date);
        pageCountClasses = Math.ceil(pageCountClasses / pageSize);
        List<Classes> classesList = classesDao.classesList(keyword, start_date, end_date, pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountClasses", pageCountClasses);
        map.put("classesList", classesList);
        return R.ok(map);
    }

    // 查询班级学生列表
    @Override
    public R classStudentList(String class_name, Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountStudents = classesDao.PageCountStudents(class_name);
        System.out.println(pageCountStudents);
        pageCountStudents = Math.ceil(pageCountStudents / pageSize);
        List<Students> classesList = classesDao.classStudentList(class_name, pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountStudents", pageCountStudents);
        map.put("classesList", classesList);
        return R.ok(map);
    }

    // 添加班级
    @Override
    public R addClasses(Classes classes) {
        LocalDate start_date = LocalDate.parse(classes.getStart_date());
        LocalDate end_date = LocalDate.parse(classes.getEnd_date());
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 比较 enrollment_date 是否小于或等于今天
        if (start_date.isBefore(today) || start_date.isEqual(today)) {
            classes.setClass_status(1);
        } else {
            classes.setClass_status(0);
        }
        if (end_date.isBefore(today) || end_date.isEqual(today))
            classes.setClass_status(2);
        return R.ok(classesDao.addClasses(classes));
    }

    @Override
    public R subjectList() {
        return R.ok(classesDao.subjectList());
    }

    @Override
    public R teacherList() {
        return R.ok(classesDao.teacherList());
    }

    // 删除学生
    @Override
    public R deleteStudent(Integer student_id) {
        return R.ok(classesDao.deleteStudent(student_id));
    }

    // 修改班级
    @Override
    public R updateClasses(Classes classes) {
        return R.ok(classesDao.updateClasses(classes));
    }

    @Override
    public R studentList() {
        return R.ok(classesDao.classStudentListByadd());
    }

    @Override
    public R addstu(String class_name,Integer student_id) {
        return R.ok(classesDao.addStudentByadd(class_name,student_id));
    }


    //教师端
    @Override
    public R classesListByTeacher(Map<String, Object> map) {
        String keyword = (String)map.get("keyword");
        String start_date = (String)map.get("start_date");
        String end_date = (String)map.get("end_date");
        int pageIndex = (int) map.get("pageIndex");
        int pageSize = (int) map.get("pageSize");
        int teacher = (int) map.get("teacher");

        pageIndex = (pageIndex - 1) * pageSize;
        double pageCount = classesDao.PageCount(teacher,keyword,start_date,end_date);
        pageCount = Math.ceil(pageCount / pageSize);
        List<Classes> classesList = classesDao.classesListByTeacher(teacher, keyword, start_date, end_date, pageIndex, pageSize);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("pageCount", pageCount);
        map2.put("classesList", classesList);
        return R.ok(map2);
    }

    @Override
    public R updateClassesByTeacher(Classes classes) {
        return R.ok(classesDao.updateClassesByTeacher(classes));
    }
}
